home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / Jump Menu.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  12.4 KB  |  381 lines

  1.  
  2. // Copyright 1999 Macromedia, Inc. All rights reserved.
  3.  
  4. //********************GLOBAL VARIABLES*************************
  5.  
  6. var helpDoc = MM.HELP_behJumpMenu;
  7.  
  8. //initialized in initGlobals()
  9.  
  10. var GlistOptions;
  11. var GtfLabel;
  12. var GtfURL;
  13. var GselTarget;
  14. var GcbRestore;
  15. var GarrMenuOptions;
  16. var GprevTarget;
  17. var GbPrevRestore;
  18. var GbValidContext;
  19.  
  20.  
  21. //function initGlobals
  22. //description: called from initializeUI(), initializes global variables
  23.  
  24. function initGlobals(){
  25.  
  26.    //initialize form widget variables:
  27.    var theForm    = document["mainLayer"].document.forms[0];
  28.    GlistOptions   = theForm.ListOptions;
  29.    GtfLabel       = theForm.Label;
  30.    GtfURL         = theForm.URL;
  31.    GselTarget     = theForm.Target;
  32.    GcbRestore     = theForm.FirstOption;
  33.    
  34.    //initialize global array used for storing menu option info
  35.    GarrMenuOptions = new Array();
  36.    
  37.    //determines if behavior can be applied given current context
  38.    GbValidContext = true;
  39.    
  40. }
  41.  
  42.  
  43. //BEHAVIOR FUNCTION(S)
  44.  
  45. //function: MM_jumpMenu
  46. //description: changes the URL of the main window or chosen frame
  47. //arguments: targ - target, it is either 'parent' or a frames reference, e.g.:
  48. //document.frames[\'myFrame\']
  49. //selObj - the menu item, always passed in as a "this" reference
  50. //restore - boolean represented as 0(false) or 1(true)
  51.  
  52. function MM_jumpMenu(targ,selObj,restore){ //v3.0
  53.   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  54.   if (restore) selObj.selectedIndex=0;
  55. }
  56.  
  57.  
  58. //***************************API FUNCTIONS************************
  59.  
  60. function canAcceptBehavior(behTag){
  61.   var retVal = false;
  62.   var selObj = dw.getBehaviorElement();
  63.   if (!selObj && behTag)
  64.     selObj = dw.getDocumentDOM().getSelectedNode();
  65.   if (selObj && selObj.tagName && selObj.tagName == "SELECT")
  66.     retVal = 'onChange';
  67.  
  68.   return retVal;
  69. }
  70.  
  71. function behaviorFunction(){
  72.    return "MM_jumpMenu";
  73. }
  74.  
  75. function identifyBehaviorArguments(){
  76.    return "other,other,other";
  77. }
  78.  
  79. //function: applyBehavior
  80. //returns Jump Menu function call with 3 arguments: target,thisObj,bRestore
  81.  
  82. function applyBehavior(){
  83.  
  84.    // if context isn't valid, don't bother with the rest
  85.    // returning an emptry string from this function means the dialog
  86.    // closes if the user clicks the OK button
  87.    // the dialog has already been populated with a friendly error message
  88.    // in the initialize UI function
  89.    if ( !GbValidContext )
  90.       return "";
  91.       
  92.    var selObj = dreamweaver.getBehaviorElement();
  93.    var selInd = selObj.selectedIndex;
  94.    var optionStr = "";
  95.    var target = escQuotes(GselTarget.options[GselTarget.selectedIndex].value);
  96.    var bRestore= (GcbRestore.checked)?1:0;
  97.    var nOptions = GarrMenuOptions.length,i;
  98.    
  99.    //update options of Jump Menu - text and the value attribute
  100.    for (i=0;i<nOptions;i++){
  101.       if (GarrMenuOptions[i][0] || GarrMenuOptions[i][1]){
  102.          optionStr += (GarrMenuOptions[i][1])?'<option value="' + GarrMenuOptions[i][1] + '">':
  103.                                              '<option>';                   
  104.          optionStr += GarrMenuOptions[i][0] + '</option>\n';
  105.       }
  106.    }
  107.    
  108.    //set new option string (optionStr) to  be innerHTML of select tag
  109.    selObj.innerHTML = optionStr;
  110.    //restore previous selected index
  111.    selObj.selectedIndex = (selInd!=-1)?selInd:0;
  112.    
  113.    
  114.    //if target or restore options have changed, 
  115.    //and this menu has one or more associated Go Buttons, the
  116.    //MM_jumpMenuGo function calls attached to these buttons
  117.    //need to be updated with the correct target and restore values.
  118.    if ( target != GprevTarget ||  bRestore != GbPrevRestore )
  119.      updateGoBtnFnCalls(target,bRestore,selObj);
  120.  
  121.    //create return value - the function call that gets attached to the event
  122.    var retVal = "MM_jumpMenu('" +
  123.                 target + "'," +    //add target argument
  124.                 "this," +          //add "this" argument
  125.                 bRestore + ")";    //add restore argument
  126.  
  127.    return retVal;
  128.    
  129. }
  130.  
  131. //function: inspectBehavior
  132. //description: passed a function call, extrapolates information
  133. //to correctly update the UI
  134.  
  135. function inspectBehavior(behFnCallStr){
  136.  
  137.    //extrapolate arguments of the function
  138.    var argArr = extractArgs(behFnCallStr);
  139.    argArr = argArr.slice(1); //first arg is function name, delete it
  140.    
  141.    //fill in Open URLs In field
  142.    selectTarget(argArr[0]);
  143.    
  144.    //check Select First Option After Chaning URL if applicable
  145.    if (argArr[2]==1)
  146.      GcbRestore.checked = true;
  147.    
  148.    //store initial values of restore and target menu --
  149.    //if they have changed when OK is clicked, MM_jumpButtonGo function
  150.    //calls on any associated Go buttons need to be changed
  151.    GprevTarget = GselTarget.options[GselTarget.selectedIndex].value;
  152.    GbPrevRestore = (GcbRestore.checked)?1:0;
  153.  
  154. }
  155.  
  156.  
  157. //********************LOCAL FUNCTIONS*************************
  158.  
  159. //function: updateGoBtnFnCalls
  160. //description: if a go button is associated with a select menu, and the
  161. //target or restore argument in the MM_jumpMenu function call is changed,
  162. //the respective arguments in any MM_jumpMenuGo function calls associated with
  163. //the current menu must be changed as well.
  164. //
  165. //Regular expression syntax is used to find all MM_jumpMenuGo function
  166. //calls in the document. The function calls are then parased to see if they are associated
  167. //with this menu, and if so, the arguments are updated, and the document regenerated
  168. //based on the new function call(s)
  169.  
  170. function updateGoBtnFnCalls(target,bRestore,selObj){
  171.  
  172.    //first of all, lets put together the pattern we are looking for:
  173.    var NSRef = escQuotes(dreamweaver.getElementRef("NS 4.0",selObj));
  174.    var IERef = escQuotes(dreamweaver.getElementRef("NS 4.0",selObj));
  175.    var pattern = escChars("MM_jumpMenuGo(" + NSRef + "," + IERef) + ",[^\\)]*\\)";
  176.    var oldFnCall = new RegExp(  pattern,"g"  );
  177.    
  178.    //then replace it with the new function call
  179.    var newFnCall = "MM_jumpMenuGo(" + NSRef + "," + IERef + "," + "'" + target
  180.                   + "'," + bRestore + ")";
  181.    var bodyNode = dreamweaver.getDocumentDOM('document').body;
  182.    var newBodStr = bodyNode.outerHTML.replace(oldFnCall,newFnCall);
  183.    
  184.    dreamweaver.getDocumentDOM('document').body.outerHTML = newBodStr;
  185.  
  186. }
  187.  
  188.  
  189. //function: escChars
  190. //description: given a text string, escapes certain characters
  191.  
  192. function escChars(theStr){
  193.   var i, theChar, escStr = "";
  194.   for(var i=0; i<theStr.length; i++) {
  195.     theChar = theStr.charAt(i);
  196.     escStr += (theChar=='"' || theChar=="'" || theChar=="\\" || theChar=="."
  197.               || theChar=="(" || theChar==")")?("\\"+theChar):theChar;
  198.   }
  199.   return escStr;
  200. }
  201.  
  202.  
  203.  
  204. //function: selectTarget
  205. //description: matches the target argument of the function call
  206. //to an existing option in the Target Menu (aka Open URLs in), and selects it. 
  207. //If not found, an option is created at the bottom of the list and selected.
  208. //default is that the "Main Window" option is selected, so it is only
  209. //changed if the target argument is a frame.
  210.  
  211. function selectTarget(target){
  212.    var frameName;
  213.    var nTargetOptions;
  214.    var i;
  215.    var bMatchFound;
  216.    
  217.    if (target != 'parent'){ //if we have a frame
  218.       //compare the target to the frame reference strings
  219.       //that are stored in the value attributes of the option tags.  
  220.       nTargetOptions = GselTarget.options.length;
  221.       bMatchFound = false;
  222.       
  223.       for (i=0;i<nTargetOptions;i++){
  224.          if (target == GselTarget.options[i].value){
  225.             GselTarget.selectedIndex = i;
  226.             bMatchFound = true;
  227.             break;
  228.          }
  229.       }
  230.       
  231.       if (!bMatchFound){
  232.          //we have a case where the target argument is not in the current frameset,
  233.          //and therefore not listed in the Target menu
  234.          //this can occur if a frame that is part of a frameset is opened in dreamweaver
  235.          //independent of that frameset. 
  236.          //Add the target argument as an option at the bottom
  237.          //of the Target menu, and select it.
  238.          frameName = extrapolateName(target);
  239.          GselTarget.options[nTargetOptions] = new Option(TYPE_Frame + '"' + frameName + '"');
  240.          GselTarget.options[nTargetOptions].value = target;
  241.          GselTarget.selectedIndex = nTargetOptions;
  242.       }
  243.    }   
  244.  
  245. }
  246.  
  247. //function: initGlobalOptionsArr
  248. //description: the global options arr, stored in the variable GarrMenuOptions,
  249. //stores all of the option label and URLs.
  250. //GarrMenuOptions is a multi-dimensional array, where
  251. //[i][0] is the text label and [i][1] is the URL value
  252. //argument: selObj - a menu object. GarrMenuOptions is created
  253. //from the text and value attributes of the menu options.
  254.  
  255. function initGlobalOptionsArr(selObj){
  256.    var optionsArr =  selObj.childNodes;
  257.    var nOptions  =   optionsArr.length;
  258.    var i;
  259.    
  260.    for (i=0;i<nOptions;i++){
  261.       GarrMenuOptions[i] = new Array(2);
  262.       GarrMenuOptions[i][0] = optionsArr.item(i).innerHTML || "";
  263.       GarrMenuOptions[i][1] = optionsArr.item(i).value || "";
  264.       if (GarrMenuOptions[i][0]=="" && GarrMenuOptions[i][1]=="") { //exists, but empty
  265.         GarrMenuOptions[i][0] = getUniqueLabel(); //create label
  266.       }
  267.    }
  268.    if (nOptions == 0){
  269.       GarrMenuOptions[0] = new Array(getUniqueLabel(),"");
  270.    }
  271. }
  272.  
  273.  
  274.  
  275. //function: initializeUI
  276. //description: initializes the UI on document onload
  277. //populates the Menu Options text area, the Label text field,
  278. //the URL text field, and the frame target menu.
  279. //also checks if the selection is valid
  280.  
  281. function initializeUI(){
  282.    
  283.     //Give users feedback if selection is not correct
  284.    //if invalid selection, display error message
  285.    //(done in inInvalidSelection function), and return
  286.    var selObj = dreamweaver.getBehaviorElement();
  287.  
  288.    if (  isInvalidSelection(selObj)  ){
  289.      GbValidContext = false;
  290.      return;
  291.    }
  292.  
  293.         
  294.    //selection is valid, so show main layer
  295.    //it starts out hidden, otherwise there is flashing
  296.    //effect from one dialog with text coming up for a few
  297.    //nanoseconds and then turning into error text
  298.    document.layers["mainLayer"].visibility = "visible";
  299.    
  300.    //initialize global variables
  301.    initGlobals();
  302.    
  303.    //populate frame target picklist, & select first one
  304.    populateFrameTargetMenu(GselTarget);
  305.    
  306.     //fill in Menu Options by populating the global options
  307.     //array and then populating the Menu Options field based
  308.     //on this array
  309.    initGlobalOptionsArr(selObj);
  310.    populateMenuOptions();
  311.    
  312.    
  313.    //fill in Label & URL fields
  314.    //if no options, add one, and put focus in label field
  315.    if ( GarrMenuOptions.length == 0) {  //if there are no options
  316.      addOption();  //add an option
  317.    }
  318.    GlistOptions.selectedIndex = 0; //select first option
  319.    GtfLabel.value = GarrMenuOptions[0][0];
  320.    GtfURL.value   = GarrMenuOptions[0][1];
  321.    GtfLabel.focus();  //put focus in Label field
  322.    GtfLabel.select(); //select Label field
  323. }
  324.  
  325. //function: checkValidity
  326. //description: this behavior can only be attached to a select menu.
  327. //checks selection and displays helpful error message in dialog
  328. //if selection is not a select menu
  329.  
  330. function isInvalidSelection(currObj){
  331.    var isValid = true,node;
  332.   
  333.    if (!currObj.tagName || currObj.tagName!="SELECT"){
  334.       isValid = false;
  335.       node = document.mainLayer;
  336.       node.innerHTML = "<BR><BR><BR><BR><div align=center><table width=80% bgcolor='#D3D3D3'><tr><td>"
  337.                              + MSG_Invalid_Selection + "<BR><BR>"
  338.                              + MSG_Correcting_Selection + "</td></tr></table></div>";
  339.       document.layers["mainLayer"].visibility = "visible";
  340.    }
  341.    return (  !isValid  );
  342.  
  343. }
  344.  
  345. //function: populateFrameTargetMenu
  346. //description: called from initializeUI, populates the frame
  347. //target menu with names of the frames in the current frameset.
  348. //if there is no frameset, populates menu with only one choice:
  349. //"Main Window"
  350.  
  351. function populateFrameTargetMenu(selectObj){
  352.    var counter = 0;
  353.    var frameList;
  354.    
  355.    selectObj.options[counter] = new Option(TYPE_MainWindow);
  356.    selectObj.options[counter++].value = 'parent';
  357.    
  358.    frameList=getObjectRefs("NS 4.0","parent","frame"); //get list of frames
  359.    if (frameList && frameList.length>0) { //if frames
  360.    //if the frame has a name, add name to target picklist
  361.       for (i=0; i<frameList.length; i++) {
  362.          if (frameList[i].indexOf('unnamed')==-1){ //if the frame has a name
  363.             frameName=extrapolateFrameName(frameList[i]);
  364.             selectObj.options[counter] = new Option(TYPE_Frame + ' "' + frameName + '"');
  365.             selectObj.options[counter++].value = frameList[i];
  366.          }
  367.       }
  368.     }
  369.    
  370.     //select first item
  371.     selectObj.selectedIndex = 0;
  372. }
  373.  
  374. // function: extraplateFrameName
  375. //description: given a frame reference, e.g: document.frames[\'myFrame\'],
  376. //returns the frame name
  377.  
  378. function extrapolateFrameName(frameRef){
  379.    return frameRef.substring(frameRef.indexOf("['")+2,frameRef.indexOf("']"));
  380. }
  381.